<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<div class="model_layer" id="model_layer">
	<div class="model_layer_bg"></div>
	<div class="model_layer_gif">
		<img src="${basePath }/img/modle.gif">
		<span>正在上传中，请稍后…</span>
	</div>
</div>

<div class="model_layer" id="attachment_layer">
	<div class="model_layer_bg"></div>
	<div class="model_layer_gif">
		<img src="${basePath }/img/modle.gif">
		<span>附件加载中，请稍后…</span>
	</div>
</div>
<script type="text/javascript">
//上传资料
var Qiniu_UploadUrl = "http://up.qiniu.com";
//var Qiniu_UploadUrl = "https://up.qbox.me";
var Qiniu_GetTokenUrl = "${basePath}/QiniuFileController/getToken";
var Qiniu_encodeUrl = "${basePath}/QiniuFileController/encodeMedia";

var default_img_upload = '${basePath}/img/shangchuanzhaopian.png';

var otherimg = '${basePath}/img/fangmian.png';

var error_image='${basePath}/img/bigSrc.png';

/**
 * 文件各类型后缀
 */
var img_extensions = "jpg,gif,png,jpeg,JPG,GIF,PNG,JPEG";
var video_extensions = "mp4,flv,avi,mp3,rm,mov,MP4,FLV,AVI,MP3,RM,MOV";
var word_extensions = "docx,doc";
var excel_extensions = "xlsx,xls";

var img_big_urls = new Array();
var index = 0;
var errorCount = 0;

//获取图片地址
function showImg(obj) {
	layer.msg('附件生成中，稍后更好看！',{
		shade: [0.5, '#000'],
		icon:16,
		time:800
    },function(){
		 preLoadImg(obj)
	});
}


function preLoadImg(obj) { 
	img_big_urls.length = 0;
	index = 0;
	errorCount = 0;
	var currentImgObj = $($(obj).parents('.button-des')[0]).siblings('img')[0];
	var currentUUID = $(currentImgObj).attr('fileid');
	
	var currentLis = $(obj).parents("ul").children("li");
	var length = currentLis.length;
    for(var i = 0; i < length; i++){
    	var xqText = $($(currentLis[i]).find('a[type="button"]')[1]).text().trim();
  		if(xqText === "详情"){
  			var imgObj = $(currentLis[i]).find('.button-des').siblings('img')[0];
  			var src = $(imgObj).attr("bigsrc");
  		    var label = $(imgObj).attr('customlabelname');
  		     var targetUUID = $(imgObj).attr('fileid');
  		     
  		     if(src && src != ''){
  		    	if(!imageExists(src)) {
  		    		src = error_image;
  		    	}
	  		    if(currentUUID == targetUUID){
	  		    	index = i - errorCount;
	  		    }
  		    	var obj = {};
  		    	obj.src = src;
  		    	obj.label = label;
  		    	img_big_urls.push(obj);
  		    } else {
  		    	errorCount++;
  		    }
  		} else {
  			errorCount++;
  		}
  	}
    layer.open({
	  	title:false,
	  	type: 2,
	  	area: ['96%', '96%'], //宽高
	  	content:'${basePath}/jsp/showAllimg.jsp'
	});
}

/**判断文件是否存在*/
function imageExists(url) {
	var xmlHttp;
	if (window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	} else if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	}
	xmlHttp.open("Get", url, false);
	xmlHttp.send();
	if (xmlHttp.status == 404) {
		return false;
	} else {
		return true;
	}
}


/**
 * fileObj 文件input
 * modeId 模块
 * ulId ul标签id
 * customLabel 是否自定义标签名
 * isAdd 是否添加li显示（true：新增li来显示图片 false:老li中图片替换）
 * fileLabelLi 添加li时使用
 * defaltLabelNameLi 添加li时使用
 * subId 子数据ID
 * attachmentTypeNames 文件类型枚举值（如果有）
 */
function uploadFile(fileObj, modeId, ulId, customLabel, isAdd, fileLabelLi, defaltLabelNameLi,subId) {
	//prompt层
	var customLabelName = null;
	if(customLabel) {
		  layer.prompt({title: '请输入文件标签，并确认', formType: 0, end:function(){
			  fileObj.value = '';
		  }}, function(text, index){
			if(text.length>5){
				 layer.tips('请输入6个以下字符', '.layui-layer-input',{tips: 2});
				return;
			}
			layer.close(index);
		    customLabelName = text;
		 	uploadFileAction(fileObj, modeId, ulId,customLabelName, customLabel, isAdd, fileLabelLi, defaltLabelNameLi,subId);
			fileObj.value = '';
		  });
	} else {
		uploadFileAction(fileObj, modeId, ulId,customLabelName, customLabel, isAdd, fileLabelLi, defaltLabelNameLi,subId);
		fileObj.value = '';
	}
}

function uploadFileAction(fileObj, modeId, ulId,customLabelName, customLabel, isAdd, fileLabelLi, defaltLabelNameLi,subId){
	var f = $(fileObj)[0].files[0];
    var filePath = fileObj.value;
    if(!filePath || filePath == '') {
    	fileObj.value = '';
    	return;
    }
    //文件名
    var fileNameIndex = filePath.lastIndexOf("\/") + 1;
    var fileName = filePath;
    if(fileNameIndex > 0){
    	fileName = fileName.substring(fileNameIndex);
    }
    //文件名
    var fileNameIndex2 = fileName.lastIndexOf("\\") + 1;
    if(fileNameIndex2 > 0){
    	fileName = fileName.substring(fileNameIndex2);
    }
    
    //文件后缀
    var subfixIndex = fileName.lastIndexOf(".") + 1;
    if(subfixIndex <= 0) {
    	layer.msg("已取消或文件格式错误", {icon: 5});
    	return;
    }
    jsJustUpload(f, fileName.substring(subfixIndex), ulId, fileObj, fileName, modeId, customLabelName, customLabel, isAdd, fileLabelLi, defaltLabelNameLi,subId);
}

function getToken(fileType,callback) {
	fileType = fileType || 'png';
	var type;
	if(''=='${attachmentTypeNames.VIDEO}') {
		type = parseAttachmentTypeAjax(fileType);
	} else {
		type = parseAttachmentType(fileType);
	}
    htmlobj = $.dpAjax({
        url: Qiniu_GetTokenUrl,
        data:{fileType:fileType,type:type},
        success:function(datas) {
        	var json = datas.result;
        	var Qiniutoken = json.token;
            var Qiniukey = json.uuid; //文件路径
            if(callback) {
            	callback(Qiniutoken, Qiniukey);
            }
        }
    });
}

function encode(fileName, mediaType, attachmentId) {
    htmlobj = $.dpAjax({
        data: {
            fileName: fileName,
            mediaType:mediaType,
            attachmentId:attachmentId
        },
        url: Qiniu_encodeUrl,
        show:false,
        success:function(datas) {
        	/* if(console) {
        		console.log(datas.result);
        	} */
        }
    });
}
//改函数要求浏览器必须要支持html5
function jsJustUpload(f, fileType, ulId, fileObj, fileName, modeId, customLabelName, customLabel, isAdd, fileLabelLi, defaltLabelNameLi,subId) {
	$('#model_layer').show();
    getToken(fileType, function(Qiniutoken, Qiniukey){
    	var xhr;
        if(window.XMLHttpRequest){  
        	xhr=new XMLHttpRequest();  
        } else if(window.ActiveXObject){  
            var activeName=["MSXML2.XMLHTTP","Microsoft.XMLHTTP"];  
            for(var i=0;i<activeName.length;i++){  
                try{  
                	xhr=new ActiveXObject(activeName[i]);  
                    break;  
                }catch(e){  
                 	if(console){
                 		console.info('ActiveXObject 创建失败');
                 	}            
                }  
            }  
        }
    	xhr.open('POST', Qiniu_UploadUrl, true);
   		var formData = new FormData();
	    if (Qiniukey && Qiniukey != '') {
	        formData.append('key', Qiniukey);
	    } else{
	     	//不管	
	    }
	    formData.append('token', Qiniutoken);
	    formData.append('file', f);
	    xhr.onreadystatechange = function (response) {
	        if (xhr.readyState == 4 && xhr.status == 200) {
	        	getUrl(ulId, fileObj, fileName, modeId, customLabelName, customLabel, isAdd, fileLabelLi, defaltLabelNameLi,subId,Qiniutoken, Qiniukey);
	        	$('#model_layer').hide();
	        } else if (xhr.status != 200 && xhr.responseText) {
	        	if(console) {
	            	console.info("服务传输异常");
	            }
	        	$('#model_layer').hide();
	        	layer.msg('上传失败', {icon: 2});
	        }
	    };
	    xhr.send(formData);
    });
}

function getUrl(ulId, fileObj, fileName, modeId, customLabelName, customLabel, isAdd, fileLabelLi, defaltLabelNameLi,subId,Qiniutoken, Qiniukey, multiUpload, fileKey, callback){
	fileKey = fileKey || Qiniukey; 
	if(!fileKey || fileKey==""){
		return;
	}
	$.dpAjax({
		show:false,
		async:false,
 		url:'${basePath}/QiniuFileController/getURL',
			data:{key:fileKey,bigImage:true},
			success: function(datas){
				var urls = datas.result;
				saveFile(ulId, fileName, urls, fileObj, modeId, customLabelName, customLabel, isAdd, fileLabelLi, defaltLabelNameLi,subId,Qiniutoken, Qiniukey, multiUpload, fileKey, callback);
         }
 	});
}

/**
 * 根据后缀分析文件类型
 * refName 文件名/文件后缀
 */
function parseAttachmentType(refName){
	if(refName && refName != '') {
		var periodInd = refName.lastIndexOf('.');
		var extension = '';
		if(periodInd >= 0) {
			extension = refName.substring(periodInd + 1);
		} else {
			extension = refName;
		}
		if(video_extensions.indexOf(extension) >= 0) {
			return '${attachmentTypeNames.VIDEO}';
		} else if(img_extensions.indexOf(extension) >= 0) {
			return '${attachmentTypeNames.IMAGE}';
		} else if(word_extensions.indexOf(extension) >= 0) {
			return '${attachmentTypeNames.WORD}';
		} else if(excel_extensions.indexOf(extension) >= 0) {
			return '${attachmentTypeNames.EXCEL}';
		}
	}
	return '${attachmentTypeNames.IMAGE}';
}

/**
 * 根据后缀分析文件类型
 * refName 文件名/文件后缀
 */
function parseAttachmentTypeAjax(refName){
	if(refName && refName != '') {
		var periodInd = refName.lastIndexOf('.');
		var extension = '';
		if(periodInd >= 0) {
			extension = refName.substring(periodInd + 1);
		} else {
			extension = refName;
		}
		if(video_extensions.indexOf(extension) >= 0) {
			return attachmentTypeNames.VIDEO;
		} else if(img_extensions.indexOf(extension) >= 0) {
			return attachmentTypeNames.IMAGE;
		} else if(word_extensions.indexOf(extension) >= 0) {
			return attachmentTypeNames.WORD;
		} else if(excel_extensions.indexOf(extension) >= 0) {
			return attachmentTypeNames.EXCEL;
		}
	}
	return attachmentTypeNames.IMAGE;
}

function parseAttachmentSort(ulId){
	if(!ulId || ulId == '') {
		return "";
	}
	if(ulId == 'SHEN_FEN_ZHENG_MING'){
		return '${attachmentSortNames.SHEN_FEN_ZHENG_MING}';
	} else if(ulId == 'ZHONG_YAO_YUAN_JIAN'){
		return '${attachmentSortNames.ZHONG_YAO_YUAN_JIAN}';
	} else if(ulId == 'PEI_OU_SHEN_FEN_ZHENG_MING'){
		return '${attachmentSortNames.PEI_OU_SHEN_FEN_ZHENG_MING}';
	} else if(ulId == 'DAN_BAO_REN_SHEN_FEN_ZHENG_MING_1'){
		return '${attachmentSortNames.DAN_BAO_REN_SHEN_FEN_ZHENG_MING_1}';
	} else if(ulId == 'DAN_BAO_REN_SHEN_FEN_ZHENG_MING_2'){
		return '${attachmentSortNames.DAN_BAO_REN_SHEN_FEN_ZHENG_MING_2}';
	} else if(ulId == 'DAN_BAO_REN_SHEN_FEN_ZHENG_MING_3'){
		return '${attachmentSortNames.DAN_BAO_REN_SHEN_FEN_ZHENG_MING_3}';
	} else if(ulId == 'DAN_BAO_REN_SHEN_FEN_ZHENG_MING_4'){
		return '${attachmentSortNames.DAN_BAO_REN_SHEN_FEN_ZHENG_MING_4}';
	} else if(ulId == 'JU_ZHU_ZHENG_MING'){
		return '${attachmentSortNames.JU_ZHU_ZHENG_MING}';
	} else if(ulId == 'YIN_HANG_LIU_SHUI'){
		return '${attachmentSortNames.YIN_HANG_LIU_SHUI}';
	} else if(ulId == 'PEI_OU_YIN_HANG_LIU_SHUI'){
		return '${attachmentSortNames.PEI_OU_YIN_HANG_LIU_SHUI}';
	} else if(ulId == 'DAN_BAO_REN_YIN_HANG_LIU_SHUI_1'){
		return '${attachmentSortNames.DAN_BAO_REN_YIN_HANG_LIU_SHUI_1}';
	} else if(ulId == 'DAN_BAO_REN_YIN_HANG_LIU_SHUI_2'){
		return '${attachmentSortNames.DAN_BAO_REN_YIN_HANG_LIU_SHUI_2}';
	} else if(ulId == 'DAN_BAO_REN_YIN_HANG_LIU_SHUI_3'){
		return '${attachmentSortNames.DAN_BAO_REN_YIN_HANG_LIU_SHUI_3}';
	} else if(ulId == 'DAN_BAO_REN_YIN_HANG_LIU_SHUI_4'){
		return '${attachmentSortNames.DAN_BAO_REN_YIN_HANG_LIU_SHUI_4}';
	} else if(ulId == 'ZHU_JIE_REN_QIAN_ZI'){
		return '${attachmentSortNames.ZHU_JIE_REN_QIAN_ZI}';
	} else if(ulId == 'PEI_OU_QIAN_ZI'){
		return '${attachmentSortNames.PEI_OU_QIAN_ZI}';
	} else if(ulId == 'JIA_FANG'){
		return '${attachmentSortNames.JIA_FANG}';
	} else if(ulId == 'DAN_WEI_FANG'){
		return '${attachmentSortNames.DAN_WEI_FANG}';
	} else if(ulId == 'TI_CHE_ZI_LIAO'){
		return '${attachmentSortNames.TI_CHE_ZI_LIAO}';
	} else if(ulId == 'KAI_KA_ZI_LIAO'){
		return '${attachmentSortNames.KAI_KA_ZI_LIAO}';
	} else if(ulId == 'YONG_HU_TOU_XIANG'){
		return '${attachmentSortNames.YONG_HU_TOU_XIANG}';
	} else if(ulId == 'YING_YE_ZHI_ZHAO'){
		return '${attachmentSortNames.YING_YE_ZHI_ZHAO}';
	} else if(ulId == 'GONG_SI_LIU_SHUI'){
		return '${attachmentSortNames.GONG_SI_LIU_SHUI}';
	} else if(ulId == 'YAN_ZI_BAO_GAO'){
		return '${attachmentSortNames.YAN_ZI_BAO_GAO}';
	} else if(ulId == 'QI_TA_ZI_LIAO_ZHAO_PIAN'){
		return '${attachmentSortNames.QI_TA_ZI_LIAO_ZHAO_PIAN}';
	} else if(ulId == 'QI_TA_ZI_LIAO_SHI_PIN'){
		return '${attachmentSortNames.QI_TA_ZI_LIAO_SHI_PIN}';
		
	//扩展字段
	} else if(ulId == 'QIAN_SHU_HE_TONG'){
		return '${attachmentSortNames.QIAN_SHU_HE_TONG}';
	} else if(ulId == 'TUI_KUAN_FU_JIAN'){
		return '${attachmentSortNames.TUI_KUAN_FU_JIAN}';
	} else if(ulId == 'GUO_HU_ZI_LIAO'){
		return '${attachmentSortNames.GUO_HU_ZI_LIAO}';
	} else if(ulId == 'TI_CHE_QI_CHE_ZI_LIAO'){
		return '${attachmentSortNames.TI_CHE_QI_CHE_ZI_LIAO}';
	} else if(ulId == 'GUO_HU_QI_CHE_ZI_LIAO'){
		return '${attachmentSortNames.GUO_HU_QI_CHE_ZI_LIAO}';
	} else if(ulId == 'GPS_ZI_LIAO'){
		return '${attachmentSortNames.GPS_ZI_LIAO}';
	}else if(ulId == 'XHH_DAI_QIAN_ZI_LIAO'){
		return '${attachmentSortNames.XHH_DAI_QIAN_ZI_LIAO}';
	}else if(ulId == 'XHH_DAI_HOU_ZI_LIAO'){
		return '${attachmentSortNames.XHH_DAI_HOU_ZI_LIAO}';
	}else if(ulId == 'XHH_FU_JIAN_DA_BAO'){
		return '${attachmentSortNames.XHH_FU_JIAN_DA_BAO}';
	}else if(ulId == 'QIAN_SHU_HE_TONG_ZILIAO'){
		return '${attachmentSortNames.QIAN_SHU_HE_TONG_ZILIAO}';
	}else if(ulId == 'XXX_QIAN_SHU_HE_TONG_ZILIAO'){
		return '${attachmentSortNames.QIAN_SHU_HE_TONG_ZILIAO}';
	}else if(ulId == 'QIAN_SHU_HE_TONG_CWLIUCHENG'){
		return '${attachmentSortNames.QIAN_SHU_HE_TONG_CWLIUCHENG}';
	}else if(ulId == 'SHEN_QING_TONG_RONG'){
		return '${attachmentSortNames.SHEN_QING_TONG_RONG}';
	}else if(ulId == 'XXX_QIAN_SHU_HE_TONG'){
		return '${attachmentSortNames.QIAN_SHU_HE_TONG}';
	}else if(ulId == 'HZ_YIN_HANG_KAIKA_ZILIAO'){
		return '${attachmentSortNames.HZ_YIN_HANG_KAIKA_ZILIAO}';
	}else if(ulId == 'HOU_BU_ZI_LIAO'){
		return '${attachmentSortNames.HOU_BU_ZI_LIAO}';
	}else if(ulId == 'XXX_HOU_BU_ZI_LIAO'){
		return '${attachmentSortNames.HOU_BU_ZI_LIAO}';
	}else if(ulId == 'HOU_ZHOU_GONG_HANG_FEN_QI_ZI_LIAO'){
		return '${attachmentSortNames.HOU_ZHOU_GONG_HANG_FEN_QI_ZI_LIAO}';
	}else if(ulId == 'XXX_HOU_ZHOU_GONG_HANG_FEN_QI_ZI_LIAO'){
		return '${attachmentSortNames.HOU_ZHOU_GONG_HANG_FEN_QI_ZI_LIAO}';
	}   
	
	//非作用域时用到 modify by zzj
	return ulId;
}

/**
 *  obj 删除标签
 *  defaltLabel 默认标签
 *  isRemove 是否删除li（true：删除li false:老li中图片属性替换默认的）
 */
function deleleFileRecord(obj, defaltLabel, isRemove){
	var imgObj = $($(obj).siblings('img')[0]);
	var fileId = imgObj.attr('fileid');
	var fileLabel = imgObj.attr('filelabel');
	layer.confirm('您确认要删除该图片吗？', {
		  btn: ['确定','取消'] //按钮
		},function(){
		layer.closeAll();
		$.dpAjax({
			show:false,
	 		url:'${basePath}/OtherAttachmentsController/remove',
			data:{id:fileId},
			success: function(datas){
			    deleteFileAction(imgObj);
			    if(isRemove) {
					 $(obj).parents('li').remove();
			    } else {
					 imgObj.removeAttr('fileShowName');
					 imgObj.removeAttr('fileid');
					 imgObj.removeAttr('filekey');
					 imgObj.removeAttr('dataid');
					 imgObj.removeAttr('subDataid');
					 imgObj.removeAttr('bigsrc');
					 imgObj.attr('src',default_img_upload);
					 imgObj.removeAttr('created');
					 imgObj.removeAttr('creatorname');
					 var labelObj = $('span[filelabelLabelSpan='+fileLabel+']');
					 imgObj.attr('customLabelName',defaltLabel);
					 var deleteObj = $('span[filelabelDelSpan='+fileLabel+']');
					 deleteObj.addClass('hidden');
					 var desObj = $('div[filelabelDiv='+fileLabel+']');
					 desObj.addClass('hidden');
					 var aDownObj = $('a[filelabelADown='+fileLabel+']');
					 aDownObj.attr('download','shangchuanzhaopian.png');
					 aDownObj.attr('href',default_img_upload);
			    }
	     }
	   });
	});	
}
function deleteFileAction(imgObj){
	    var fileKey = $(imgObj).attr('filekey');
	    $.dpAjax({
			show:true,
	 		url:'${basePath}/QiniuFileController/removeFile',
			data:{fileName:fileKey},
			success: function(datas){
 				 layer.msg("删除文件成功!", {icon: 6});
         	}
 	 });
}

//两个参数，第一个是要格式化的字符串，第二个是以什么分割符将年，月，日连接  
function timestamp2Time(timestamp, separator) {  
    var result = "";  
      
    if(timestamp) {  
        var reg = new RegExp(/\D/, "g"); //提取数字字符串  
        var timestamp_str = timestamp.replace(reg, "");  

        var d = new Date();  
        d.setTime(timestamp_str);  
        var year = d.getFullYear();  
        var month = d.getMonth() + 1;  
        var day = d.getDate();  
        if(month < 10) {  
            month = "0" + month;  
        }  
        if(day < 10) {  
            day = "0" + day;  
        }  
        result = year + separator + month + separator + day;  
    }  
    return result;  
}  

var default_img = '${basePath}/img/zhengmian.png';

/**
 * dataid : 数据id
 * customerId： 客户id
 * modeId ：所处阶段
 * subId :子数据id
 * fn 回调函数
 * excludeFileLabels: 排除的label
 * includeFileLabels: 附件label
 */
function showAttentment(dataId, customerId, modeId, subId, fn, excludeFileLabels, includeFileLabels) {
	 $('#attachment_layer').show();
	 modeId = modeId || '';
	 var data = {id: dataId, customerId:customerId, modeId:modeId}
	 if(subId && subId != '') {
		 data.subDataId = subId;
	 }
	 if(includeFileLabels && includeFileLabels != ''){
		 data.includeFileLabels = includeFileLabels;
	 }
	 if(excludeFileLabels && excludeFileLabels != ''){
		 data.excludeFileLabels = excludeFileLabels;
	 }
	//展示数据
   	$.dpAjax({
   		show:false,
   		url:'${basePath}/OtherAttachmentsController/queryOtherAttachments',
   		data:data,
		success: function(datas){
   				setTimeout(function(){
   					$('#attachment_layer').hide();
   				},500);
   				 var result = datas.result;
   				 //附件信息
   				 var otherAttachments = result.otherAttachments;
   				 if(otherAttachments) {
   					 showFj(otherAttachments);
    				 if(fn){
    					 fn(otherAttachments);
    				 }
   				 }
   			  	$('#attachment_layer').hide();
          },
          error:function(err){
        	  $('#attachment_layer').hide(); 
        	   waitingDialog.hide();
          }
 });
}

//展示附件
function showFj(otherAttachmentses, fn) {
		 for(var key in otherAttachmentses) {
			 var otherAttachmentseParts = otherAttachmentses[key];
			 showSort(otherAttachmentseParts, key);
			 if(key == 'QIAN_SHU_HE_TONG_ZILIAO') {
				 showSort(otherAttachmentseParts, 'XXX_QIAN_SHU_HE_TONG_ZILIAO');
			 }else if(key == 'QIAN_SHU_HE_TONG'){
				 showSort(otherAttachmentseParts, 'XXX_QIAN_SHU_HE_TONG');
			 }else if(key == 'HOU_BU_ZI_LIAO'){
				 showSort(otherAttachmentseParts, 'XXX_HOU_BU_ZI_LIAO');
			 }else if(key == 'HOU_ZHOU_GONG_HANG_FEN_QI_ZI_LIAO'){
				 showSort(otherAttachmentseParts, 'XXX_HOU_ZHOU_GONG_HANG_FEN_QI_ZI_LIAO');
			 }
		 }
		 if(fn){
			 fn();
		 }
 }
 
 function showSort(otherAttachmentseParts, key) {
	 var fileLabels = "";
	 if(otherAttachmentseParts && otherAttachmentseParts.length > 0) {
		 for(var i = 0; i < otherAttachmentseParts.length; i++) {
			 var otherAttachmentsePart = otherAttachmentseParts[i];
			 var id = key;
			 
			 var attachetype='${attachmentTypeNames.IMAGE}';
			 //判断是否从作用域取值 modify by zzj
			 var url = otherAttachmentsePart.url;
			 var isImage = false;
			 if(attachetype && attachetype!=""){
				 if(otherAttachmentsePart.attachmentType != '${attachmentTypeNames.IMAGE}') {
					 url = default_img;
				 } else {
					 isImage = true;
				 }
			 }else{
				 if(otherAttachmentsePart.attachmentType != attachmentTypeNames.IMAGE && attachmentTypeNames.IMAGE!=""){
					 url = default_img;
				 } else {
					 isImage = true;
				 }
			 }
			 var fileLabel = otherAttachmentsePart.fileLabel;
			 var customLabelName = otherAttachmentsePart.customLabelName;
			 var imgObj = $('#' + key + ' img[filelabel='+fileLabel+']');
			 var bigUrl = otherAttachmentsePart.bigUrl;
			 
			 if(imgObj && imgObj.length == 1 && fileLabels.indexOf(fileLabel) < 0){
				 imgObj.attr('fileShowName',otherAttachmentsePart.fileShowName);
				 imgObj.attr('filelabel',fileLabel);
				 imgObj.attr('fileid',otherAttachmentsePart.replaceId);
				 imgObj.attr('filekey',otherAttachmentsePart.ossPath);
				 imgObj.attr('bigsrc',otherAttachmentsePart.bigUrl);
				 imgObj.attr('dataid',otherAttachmentsePart.dataid);
				 if(otherAttachmentsePart.subDataid){
				 	imgObj.attr('subDataid',otherAttachmentsePart.subDataid);
				 }
				 imgObj.attr('src',url);
				 imgObj.attr('created',otherAttachmentsePart.created);
				 imgObj.attr('creatorname', otherAttachmentsePart.creatorName);
				 
				 var labelObj = $('#' + key + ' span[filelabelLabelSpan='+fileLabel+']');
			     if(customLabelName) {
					imgObj.attr('customLabelName',customLabelName);
				 	labelObj.text(customLabelName);
			     }
				 var deleteObj = $('#' + key + ' span[filelabelDelSpan='+fileLabel+']');
				 deleteObj.removeClass('hidden');
				 var desObj = $('#' + key + ' div[filelabelDiv='+fileLabel+']');
				 desObj.removeClass('hidden');
				 var aDownObj = $('#' + key + ' a[filelabelADown='+fileLabel+']');
				 aDownObj.attr('download',otherAttachmentsePart.fileShowName);
				 aDownObj.attr('href',otherAttachmentsePart.bigUrl);
			 } else { //插入li
				 var fileInputLis = $('#'+id).find('.zx-upimg');
				 var flag = true; //true 信息展示页面  false 资料编辑页面
				 if(fileInputLis && fileInputLis.length > 0) {
					 flag = false;
				 }
	    		 
	    		 var html = '<li><img';
	    		 if(customLabelName) {
	    			 html += ' customLabelName="' + customLabelName + '"';
	    		 }
	    		 html += ' fileShowName="'+otherAttachmentsePart.fileShowName+'"';
	    		 html += ' filelabel="'+fileLabel+'"';
	    		 html += ' fileid="'+otherAttachmentsePart.replaceId+'"';
	    		 html += ' filekey="' + otherAttachmentsePart.ossPath + '"';
	    		 html += ' bigsrc="' +otherAttachmentsePart.bigUrl+'"';
	    		 html += ' dataid="' +otherAttachmentsePart.dataid +'"';
	    		 if(otherAttachmentsePart.subDataid){
	    		 	html += ' subDataid="' +otherAttachmentsePart.subDataid+'"';
	    		 }
	    		 html += ' src="' + url + '"';
	    		 html += ' created="' + otherAttachmentsePart.created + '"';
	    		 html += ' creatorname="'+ otherAttachmentsePart.creatorName + '"';
	    		 html += ' width="110px" height="110px" class="zx-imgPre ' + fileLabel + '"/>';
	    		 if(!flag) {
		    		 html += ' <span filelabelDelSpan="' + fileLabel +'" class="bt-cancle glyphicon glyphicon-trash"';
		    		 html += ' onclick="deleleFileRecord(this,\'' + fileLabel + '\', true);"></span>';
	    		 }
	    		 html += '<div class="img-layer"><div></div>';
	    		 html += ' <span filelabelLabelSpan="' + fileLabel +'">' + customLabelName +'</span></div>';
	    		 html += '<div filelabelDiv="' + fileLabel + '" class="button-des">';
	    		 html += '<a type="button" class="abuttonStylele" filelabelADown="' + fileLabel + '" download="' + otherAttachmentsePart.fileShowName + '" href="' + otherAttachmentsePart.bigUrl + '" >下载</a>';
	    		 if(isImage && url && url != default_img) {
	    		 	html += '<a type="button" class="abuttonStyleri"  onclick="showImg(this);">详情</a>';
	    		 }
	    		 html += '</div></li>';
	    		 //检查重复
	    		 var $li = $('#' + key + ' img[fileid="' + otherAttachmentsePart.replaceId + '"]').parent('li');
				 $li.remove();
				 
				 if(!flag) {
				 	var fileInputLi = $(fileInputLis[0]);
				 	$(html).insertBefore(fileInputLi);
				 } else {
					 $('#'+id).append(html);
				 }
			 }
			 fileLabels += fileLabel;
		 }
	 }
 }

//保存文件记录
function saveFile(ulId,fileName, urls, fileObj, modeId, customLabelName, customLabel, isAdd, fileLabelLi, defaltLabelNameLi,subId,Qiniutoken, Qiniukey, multiUpload, fileKey, callback){
	saveFileAction(ulId,fileName, urls, fileObj, modeId, customLabelName, customLabel, isAdd, fileLabelLi, defaltLabelNameLi,subId,Qiniutoken, Qiniukey, multiUpload, fileKey, callback);
}
//保存文件
function saveFileAction(ulId,fileName,urls,fileObj, modeId, customLabelName, customLabel, isAdd, fileLabelLi, defaltLabelNameLi,subId,Qiniutoken, Qiniukey, multiUpload, fileKey, callback){
	var data = {};
	fileKey = fileKey || Qiniukey;
	if(subId && subId != ''){
		data.subDataid = subId;
	}
	data.modeId = modeId; // 所处阶段
	data.dataid = $("#busiId").val(); // 所属业务
	data.customerId = $("#customerId").val(); // 所属客户
	data.attachmentSort = parseAttachmentSort(ulId);
	data.attachmentType = parseAttachmentType(fileKey);
	//非作用域时用到 modify by zzj
	if(data.attachmentType==null || data.attachmentType==""){
		data.attachmentType=parseAttachmentTypeAjax(fileKey);
	}
	var fileLabel = null;
	if(isAdd) {
		fileLabel = fileLabelLi;
	} else {
		fileLabel = $(fileObj).parents('a').siblings('img').attr('filelabel');
	}
	data.fileLabel = fileLabel;
	if(customLabelName) {
		data.customLabelName = customLabelName;
	}
	data.fileName = fileKey;
	data.fileShowName = fileName;
	data.ossPath = fileKey;
	data.fileHandleStatus = '01';
	data.clientType = 'pc';
	
	//程立勇  2017 06 13
	var upDelete = $('#upDelete').val();
	if(!upDelete || upDelete == "" || null == upDelete || upDelete == "null" || undefined == upDelete || upDelete == "upDelete")
	{
		upDelete = "0";
	}
	data.upDelete = upDelete;
	//程立勇  2017 06 13
	
	$.dpAjax({
		show:false,
		async:false,
 		url:'${basePath}/OtherAttachmentsController/save',
		data:data,
		success: function(datas){
			 var result = datas.result;
			 var url = urls.url;
			 var attachetype='${attachmentTypeNames.IMAGE}';
			 //判断是否从作用域取值 modify by zzj
			 var isImage = false;
			 if(attachetype && attachetype != ""){
				 if(data.attachmentType != '${attachmentTypeNames.IMAGE}') {
					 url = default_img;
				 } else {
					 isImage = true;
				 }
			 }else{
				 if(data.attachmentType != attachmentTypeNames.IMAGE && attachmentTypeNames.IMAGE!=""){
					 url = default_img;
				 } else {
					 isImage = true;
				 }
			 }
			 var customLabelName = result.customLabelName
			 if(isAdd) { //新增li
				var html = '<li><img customLabelName="'+customLabelName+'" fileShowName="'+fileName+'" filelabel="'+result.fileLabel+'"fileid="'+result.replaceId+'" filekey="' 
				+ fileKey + '" bigsrc="' +urls.bigUrl + '" src="' + url + '" created="' + result.created 
				+ '" creatorname="'+ result.creatorName+'" dataid="'+result.dataid + '"';
				if(result.subDataid){
					html += ' subDataid="'+result.subDataid + '"';
				}
				html += ' width="110px" height="110px" class="zx-imgPre ' + fileLabel + '" />';
				html += '<span filelabelDelSpan="' +fileLabel+ '" class="bt-cancle glyphicon glyphicon-trash" onclick="deleleFileRecord(this,\''+ defaltLabelNameLi + '\',true);"></span>';
				html += '<div class="img-layer"><div></div><span filelabelLabelSpan="' + fileLabel +'">' + customLabelName + '</span></div>';
				html += '<div filelabelDiv="' + fileLabel + '" class="button-des"><a class="abuttonStylele" type="button" filelabelADown="' + fileLabel +'" download="' + fileName;
				html += '" href="' + urls.bigUrl + '" >下载</a>';
				if(isImage) {
					html += '<a class="abuttonStyleri" type="button" onclick="showImg(this);">详情</a>';
				}
				html += '</div></li>';
				//兼容多个上传控件 熊晓豚 2017/6/14
				var uls = $(fileObj).parents('ul');
				if(multiUpload) { //多文件上传插件
					//在当前UL的第一个多文件传按钮之前加入
					var firstMuliUploadButtons = $('#'+ulId).find('.firstMuliUploadButton');
					if(firstMuliUploadButtons && firstMuliUploadButtons.length > 0) {
						$(html).insertBefore($(firstMuliUploadButtons[0]));
					} else {
						if(console) {
							console.info("第一个多文件上传按钮样式缺失，请添加class:firstMuliUploadButton");
						}
					}
				} else {
					var upimgs = uls.children('li.zx-upimg');
					if(upimgs && upimgs.length > 0) {
						$(html).insertBefore($(upimgs[0]));
					} else {
						if(console) {
							console.info("上传li缺失，请添加class:zx-upimg");
						}
					}
				}
			 } else {//展示全部图片
				 var bigUrl =  urls.bigUrl;
				//展示图片
				 var imgObj = $('img[filelabel='+fileLabel+']');
				 var oldFileid = imgObj.attr('fileid');
				 var oldFilekey = imgObj.attr('filekey');
				 imgObj.attr('customlabelname',customLabelName);
				 imgObj.attr('fileShowName',fileName);
				 imgObj.attr('filelabel',result.fileLabel);
				 imgObj.attr('fileid',result.replaceId);
				 imgObj.attr('filekey',fileKey);
				 imgObj.attr('bigsrc',urls.bigUrl);
				 imgObj.attr('dataid',result.dataid);
				 if(result.subDataid){
				 	imgObj.attr('subDataid',result.subDataid);
				 }
				 imgObj.attr('src',url);
				 imgObj.attr('created',result.created);
				 imgObj.attr('creatorname',result.creatorName);
				 
				 var labelObj = $('span[filelabelLabelSpan='+fileLabel+']');
			     if(customLabel) {
					imgObj.attr('customLabelName',customLabelName);
				 	labelObj.text(customLabelName);
			     }
				 var deleteObj = $('span[filelabelDelSpan='+fileLabel+']');
				 deleteObj.removeClass('hidden');
				 var desObj = $('div[filelabelDiv='+fileLabel+']');
				 desObj.removeClass('hidden');
				 var aDownObj = $('a[filelabelADown='+fileLabel+']');
				 aDownObj.attr('download',fileName);
				 aDownObj.attr('href',urls.bigUrl);
				 
				 if(oldFileid && oldFileid != '') {
					 deleteRecordFileById(oldFileid,oldFilekey);
				 }
		    }
			 
			//请求任务处理 2017-6-26 熊晓豚
			var attachmentId = result.replaceId;
			if(''=='${attachmentTypeNames.VIDEO}') {
				var mediaType = parseAttachmentTypeAjax(fileKey);
				if(mediaType == attachmentTypeNames.IMAGE || mediaType == attachmentTypeNames.VIDEO) {
					encode(fileKey,mediaType,attachmentId);
				}
			} else {
				var mediaType = parseAttachmentType(fileKey);
				if(mediaType == '${attachmentTypeNames.VIDEO}' || mediaType == '${attachmentTypeNames.IMAGE}') {
					encode(fileKey,mediaType,attachmentId);
				}
			}
			if(callback) {
				callback();
			}
			if(!multiUpload) {
		 		layer.msg("上传文件成功!", {icon: 6});
			}
		 	
     }
	});
}

//替换文件
function deleteRecordFileById(id,filekey){
	$.dpAjax({
		show:false,
 		url:'${basePath}/OtherAttachmentsController/remove',
		data:{id:id},
		success: function(datas){
			layer.closeAll();
			deleteActionByFileKey(filekey);
     	}
	});
}

function deleteActionByFileKey(filekey){
	$.dpAjax({
		show:false,
 		url:'${basePath}/QiniuFileController/removeFile',
		data:{fileName:filekey},
		success: function(datas){
			layer.closeAll();
     	}
	});
}
</script>

